package com.example.data_show.controller;

import com.example.data_show.entity.FaceEngineTest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.Map;

@RestController
@RequestMapping("/AboutMarketing")
public class ImageController {
    @PostMapping("/checkFace")
    public boolean checkFace(@RequestBody Map<String, String> requestBody) {

        String photoData = requestBody.get("photo");
        // 截取Base64编码数据中的实际图片数据部分（去掉 data:image/jpeg;base64, 的部分）
        String base64Image = photoData.split(",")[1];

        // 将Base64编码字符串解码为字节数组
        byte[] decodedBytes = Base64.getDecoder().decode(base64Image);

        // 设置保存图片的路径和文件名
        String filePath = "E:\\springboot_learning\\data_show\\check\\image.jpg";

        try {
            // 将解码后的字节数组写入文件
            FileOutputStream fileOutputStream = new FileOutputStream(new File(filePath));
            fileOutputStream.write(decodedBytes);
            fileOutputStream.close();


            FaceEngineTest faceEngineTest = new FaceEngineTest();
            boolean result = faceEngineTest.checkFace();

            File fileToDelete = new File(filePath);
            fileToDelete.delete();

            return result;
        } catch (IOException e) {
            System.out.println("保存图片时出错：" + e.getMessage());
            return false;
        }
    }
}
